import 'package:aichat_flutter_wallet/module/module_dapp/widgets/components/dapp_tip_com.dart';
import 'package:aichat_flutter_wallet/module_common/bridge/native_channel_bridge.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_network/http_config.dart';
import 'package:lib_network/http_manager.dart';
import 'package:lib_util/lib_util.dart';
import 'package:lib_util/log/log.dart';
import '../../../module_common/network/http_api.dart';
import '../../../module_common/widgets/header/page_header.dart';
import '../../../module_common/widgets/image/public_image.dart';
import '../../../module_common/widgets/page_status_com.dart';
import '../../module_browser/viewmodel/before_browser.dart';
import '../model/dapp_hot_model.dart';
import '../provider/dapp_provider.dart';
import '../utils/dapp_favorite_util.dart';
import '../widgets/hover_status.dart';
import 'package:lib_database/src/bean.dart';
import 'package:aichat_flutter_wallet/module_common/utils/util.dart';

class DAppHotPage extends StatefulWidget {
  const DAppHotPage({super.key, required this.title});

  final String title;

  @override
  State<DAppHotPage> createState() => _DAppHotPageState();
}

class _DAppHotPageState extends State<DAppHotPage> {
  List<BookMarkListModel> compList = [];

  @override
  void initState() {
    super.initState();
    // fetchData();
  }

  Future<void> fetchData() async {
    try {
      var params = {"type": 0, "id": 0};
      var resp = await HttpManager().post(API.walletDAppHot, params);
      if (resp?.code == HttpConfig().successCode) {
        var model = DAppWalletHotModel.fromJson(resp!.data);
        final BookMarkListModel hotDataList = model.bookmarkList!.firstWhere((element) {
          return element.bookmark.name == 'hot';
        });
        if (!(hotDataList.bookmark != null)) {
          return;
        }
        var resultList = hotDataList.children?[0].children?.toList() ?? [];
        if (resultList.length > 10) {
          resultList = resultList.sublist(0, 10);
        }
        setState(() {
          compList = resultList;
        });
      }
    } catch (e) {
      Log.logger.e(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    final userId = NativeChannelBridge.instance.appCommMsg?.userId ?? '';
    return Scaffold(
        //body占满剩余空间 可滚动
        body: Consumer<DAppProvider>(builder: (c, model, child) {
      final List<DAppBean> favoriteDAppList = model.favoriteDAppList.toList();
      return Container(
          color: Color.fromRGBO(255, 255, 255, 1),
          child: Column(children: [
            PageHeaderCom(
              title: 'Hot',
            ),
            if (model.hotList.length > 0)
              Expanded(
                flex: 1,
                child: SizedBox(
                  child: ListView.builder(
                    padding: EdgeInsets.all(0),
                    itemCount: model.hotList.length + 1,
                    itemBuilder: (BuildContext buildContext, int index) {
                      if (index < model.hotList.length) {
                        BookMarkListModel item = model.hotList[index];
                        final DAppBean? favoriteDAppItem = (favoriteDAppList.length > 0
                            ? favoriteDAppList.firstWhereOrNull(
                                (element) => element.url == item.bookmark.url,
                              )
                            : null);
                        bool isLove = favoriteDAppItem != null;
                        return HoverStatus(
                          defaultColor: Color.fromRGBO(255, 255, 255, 1),
                          hoverColor: Color.fromRGBO(246, 246, 246, 1),
                          onPressed: () {
                            doGotoBrowser(context: context, webUrl: item.bookmark.url);
                          },
                          child: Container(
                              padding: EdgeInsets.only(left: 20.w, top: 14.w, bottom: 14.w, right: 28.w),
                              child: Stack(
                                children: [
                                  Row(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Padding(
                                        padding: EdgeInsets.only(right: 10.w),
                                        child: ClipRRect(
                                          borderRadius: BorderRadius.circular(46.w), // 设置圆角半径
                                          child: PublicImage(image: item.bookmark.logo, width: 46.w, height: 46.w),
                                        ),
                                      ),
                                      Expanded(
                                          child: Column(
                                        mainAxisSize: MainAxisSize.max,
                                        // mainAxisAlignment: MainAxisAlignment.center,
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Row(
                                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                            children: [
                                              Row(
                                                children: [
                                                  Padding(
                                                    padding: EdgeInsets.only(right: 4.w),
                                                    child: Text(
                                                      item.bookmark.name,
                                                      style: TextStyle(
                                                        fontSize: 14.sp,
                                                        fontWeight: FontWeight.w500,
                                                        color: Color.fromRGBO(51, 51, 51, 1),
                                                      ),
                                                    ),
                                                  ),
                                                  if (item.bookmark.statement != '')
                                                    DappTips(title: item.bookmark.statement),
                                                  // Container(
                                                  //   padding:
                                                  //       EdgeInsets.all(3),
                                                  //   decoration: BoxDecoration(
                                                  //     borderRadius:
                                                  //         BorderRadius.only(
                                                  //             topLeft:
                                                  //                 Radius
                                                  //                     .circular(
                                                  //                         7),
                                                  //             topRight: Radius
                                                  //                 .circular(
                                                  //                     7),
                                                  //             bottomRight:
                                                  //                 Radius
                                                  //                     .circular(
                                                  //                         7)),
                                                  //     color: Colors.white,
                                                  //     gradient:
                                                  //         LinearGradient(
                                                  //       begin:
                                                  //           Alignment.topLeft,
                                                  //       end: Alignment
                                                  //           .bottomRight,
                                                  //       colors: [
                                                  //         Color.fromRGBO(185,
                                                  //             156, 255, 1),
                                                  //         Color.fromRGBO(122,
                                                  //             88, 255, 1),
                                                  //       ],
                                                  //     ),
                                                  //   ),
                                                  //   child: Text(
                                                  //     item.bookmark.statement,
                                                  //     style: TextStyle(
                                                  //       fontSize: 8,
                                                  //       fontWeight:
                                                  //           FontWeight.w500,
                                                  //       color: Color.fromRGBO(
                                                  //           255, 255, 255, 1),
                                                  //     ),
                                                  //   ),
                                                  // )
                                                ],
                                              ),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 5.w,
                                          ),
                                          Text(item.bookmark.url,
                                              style: TextStyle(
                                                fontSize: 12.sp,
                                                fontWeight: FontWeight.w400,
                                                color: Color.fromRGBO(153, 153, 153, 1),
                                              )),
                                          SizedBox(
                                            height: 7.w,
                                          ),
                                          Text(item.bookmark.introduce as String,
                                              style: TextStyle(
                                                fontSize: 12.sp,
                                                fontWeight: FontWeight.w400,
                                                color: Color.fromRGBO(134, 131, 145, 1),
                                              )),
                                        ],
                                      )),
                                    ],
                                  ),
                                  Positioned(
                                      right: 0,
                                      top: 0,
                                      bottom: 0,
                                      child: IgnorePointer(
                                          ignoring: false,
                                          child: GestureDetector(
                                            onTap: () {
                                              DAppFavoriteUtil.showSetFavorite(context,
                                                  DApp: DAppBean(
                                                      userId,
                                                      item.bookmark.name,
                                                      item.bookmark.logo,
                                                      item.bookmark.url,
                                                      item.bookmark.introduce,
                                                      [1],
                                                      item.bookmark.statement));
                                            },
                                            child: Container(
                                              padding: EdgeInsets.only(left: 30.w, bottom: 30.w, top: 0, right: 0),
                                              child: Image.asset(
                                                isLove
                                                    ? 'assets/images/dapp/star-active.png'
                                                    : 'assets/images/dapp/star.png',
                                                width: 16.w,
                                                height: 16.w,
                                              ),
                                            ),
                                          )))
                                ],
                              )),
                        );
                      }
                      return Container(
                        height: 70.w,
                        alignment: Alignment.center,
                        child: Text(
                          'Already at the bottom',
                          style: TextStyle(
                              fontSize: 14.sp, color: Color.fromRGBO(190, 189, 202, 1), fontWeight: FontWeight.w400),
                        ),
                      );
                    },
                  ),
                ),
              ),
            if (model.hotList.length == 0)
              Container(
                margin: EdgeInsets.only(top: 100.w),
                child: PageEmptyCom(type: 'empty'),
              )
          ]));
    }));
  }
}
